package org.jeecg.modules.basedata.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.SsoUtil;
import org.jeecg.common.util.StringUtils;
import org.jeecg.entity.basedata.BaseCustomer;
import org.jeecg.entity.basedata.BaseRoom;
import org.jeecg.modules.basedata.mapper.BaseCustomerMapper;
import org.jeecg.modules.basedata.mapper.BaseProjectMapper;
import org.jeecg.modules.basedata.mapper.BaseRoomMapper;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.model.DepartIdModel;
import org.jeecg.modules.system.model.SysDepartTreeModel;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.util.FindsDepartsChildrenUtil;
import org.jeecg.modules.basedata.service.IBaseCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.vo.basedata.BaseCustomerPage;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 业主
 * @Author: jeecg-boot
 * @Date:   2019-07-18
 * @Version: V1.0
 */
@Service
public class BaseCustomerServiceImpl extends ServiceImpl<BaseCustomerMapper, BaseCustomer> implements IBaseCustomerService {

    @Autowired
    private BaseCustomerMapper baseCustomerMapper;
    @Autowired
    private BaseProjectMapper baseProjectMapper;
    @Autowired
    private ISysUserRoleService sysUserRoleService;
    @Autowired
    private BaseRoomMapper baseRoomMapper;



    @Override
    public Page<BaseCustomerPage> listHistory(Page<BaseCustomerPage> page, BaseCustomer baseCustomer) {
        return  page.setRecords(baseCustomerMapper.listHistory(page, baseCustomer));
    }

    @Override
    public BaseCustomer queryByRoomId(String roomId) {
        return baseCustomerMapper.queryByRoomId(roomId);
    }

    @Override
    public Integer listCustomerId(String roomid) {
        return baseCustomerMapper.listCustomerId(roomid);
    }

    @Override
    public BaseCustomer detailById(String id, String methodId) {
        return baseCustomerMapper.detailById(id, methodId);
    }

    @Override
    public Page<BaseCustomerPage> listByParams(Page<BaseCustomerPage> page, BaseCustomerPage baseCustomer) {
        return page.setRecords(baseCustomerMapper.listByParams(page,baseCustomer));
    }

    /***********************************租户*****************************************************/
    @Override
    public Page<BaseCustomer> getBaseTenantList(Page<BaseCustomer> page, Wrapper<BaseCustomer> wrapper) {
        return page.setRecords(baseCustomerMapper.getBaseTenantPage(page, wrapper));
    }

    @Override
    public List<SysDepartTreeModel> queryTenantTreeList(String menuPath) {
        List<SysDepartTreeModel> list = new ArrayList<>();
        if (StringUtils.isNotBlank(menuPath)) {
            //根据权限加载
            List<SysDepart> departTreeList = sysUserRoleService.queryDepartTreeList(menuPath, SsoUtil.getLoginUser().getId(), CommonConstant.DEPART_WHERE_CO);
            List<String> departIdList = new ArrayList<>();
            Map<String, SysDepart> departTreeMap = new HashMap<>();
            List<SysDepartTreeModel> branchList = new ArrayList<>();
            for (SysDepart departTree : departTreeList) {
                departTreeMap.put(departTree.getId(), departTree);
                departIdList.add(departTree.getId());
                SysDepartTreeModel branch = new SysDepartTreeModel(departTree);
                branchList.add(branch);
            }
            List<DepartIdModel> idList = new ArrayList<>();
            for (SysDepart depart : departTreeList) {
                if (depart.getType().equals(CommonConstant.DEPART_PROPERTY_DEPARTMENT)) continue;
                if ((StringUtils.isBlank(depart.getParentId())) || (StringUtils.isNotBlank(depart.getParentId()) && !departTreeMap.containsKey(depart.getParentId()))) {
                    SysDepartTreeModel branch = new SysDepartTreeModel(depart);
                    list.add(branch);
                    DepartIdModel departIdModel = new DepartIdModel().convert(branch);
                    idList.add(departIdModel);
                }
            }
            if (departTreeList.size() > 0) {
                if (departTreeList.size() > 0) {
                    List<SysDepart> projectTreeList = sysUserRoleService.queryProjectTreeListByDepartId(departIdList);
                    for (SysDepart project : projectTreeList) {
                        SysDepartTreeModel branch = new SysDepartTreeModel(project);
                        List<SysDepartTreeModel> listAll = baseProjectMapper.queryUnitTreeList(project.getId());
                        if (listAll != null && listAll.size() > 0) {
                            for (SysDepartTreeModel treeModel : listAll) {
                                List<SysDepartTreeModel> roomList = baseCustomerMapper.queryTenantTreeList(treeModel.getId());
                                if (roomList != null && roomList.size() > 0) {
                                    for (SysDepartTreeModel room : roomList) {
                                        room.setKey(room.getId());
                                        room.setValue(room.getId());
                                        room.setIsLeaf(true);
                                        room.setTitle(room.getDepartName());
                                    }
                                    branchList.addAll(roomList);
                                }
                            }
                            branchList.addAll(listAll);
                        }
                        branchList.add(branch);
                    }
                    FindsDepartsChildrenUtil.getGrandChildren(list, branchList, idList);
                    FindsDepartsChildrenUtil.setEmptyChildrenAsNull(list);
                }
            }
        }
        return list;
    }

    @Override
    public List<SysDepartTreeModel> querFloorTreList() {
        LambdaQueryWrapper<SysDepart> query = new LambdaQueryWrapper<>();
        query.eq(SysDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
        query.eq(SysDepart::getDepartProperty,CommonConstant.DEPART_PROPERTY_COMPANY);
        query.orderByAsc(SysDepart::getDepartOrder);
        List<SysDepart> list = baseCustomerMapper.querFloorTreList();
        // 调用wrapTreeDataToTreeList方法生成树状数据
        List<SysDepartTreeModel> listResult = FindsDepartsChildrenUtil.wrapTreeDataToTreeList(list,"floor");
        return listResult;
    }

    @Override
    public String  getByTenantId(String roomId){
        String  id = baseCustomerMapper.getByTenantId(roomId);
        return id;
    }

    @Override
    public BaseCustomer getTenantByRoomId(String roomId) {
        return baseCustomerMapper.getTenantByRoomId(roomId);
    }

    @Override
    public String  getByCustomerId(String roomId){
        String  id = baseCustomerMapper.getByCustomerId(roomId);
        return id;
    }

    @Override
    public int getCountByRoomIdAndCustomerName(String roomId, String customerName) {
        return baseCustomerMapper.getCountByRoomIdAndCustomerName(roomId,customerName);
    }

    @Override
    public int getCountByRoomId(String roomId) {
        return baseCustomerMapper.getCountByRoomId(roomId);
    }

    @Override
    public List<BaseCustomer> getCustomer(BaseCustomer baseCustomer) {
        return baseCustomerMapper.getCustomer(baseCustomer);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addBatchCustomer(List<BaseCustomer> baseCustomers, List<BaseRoom> baseRoomList) {
        try {
            this.saveBatch(baseCustomers);
            if (baseRoomList.size() > 0){
                baseRoomMapper.updateBatchRoomStatus(baseRoomList);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw e;
        }
    }

    @Override
    public Integer getCustomerCount(String roomId) {
        return baseCustomerMapper.getCustomerCount(roomId);
    }

}
